import { Component,OnDestroy } from '@angular/core';

@Component({
  selector: 'app-child3',
 template: '<p>{{message}}</p>'
})
export class Child3Component implements OnDestroy{

  intervalId = 0;
  message = '';
  seconds = 11;

  ngOnDestroy() {
    this.clearTimer();
  }

  start() {
    this.countDown();
  }
  stop() {
    this.clearTimer();
    this.message = `停在${this.seconds}秒`;
  }

  private clearTimer() {
    clearInterval(this.intervalId);
  }

  private countDown() {
    this.clearTimer();
    this.intervalId = window.setInterval(() => {
      this.seconds -= 1;
      if (this.seconds === 0) {
        this.message = '发射！';
      }
      else {
        if (this.seconds < 0) {
          this.seconds = 10;//重置
        }
        this.message =  `${this.seconds}秒开始数`;
      }
    },1000);
  }

 

}
